#pragma once

#include "../QDrawCell/qshapedrawcell.h"
#include "../QImage/qimage.h"
#include "../QFont/qfont.h"
#include "../QWidget/qwidget.h"

#include <string>

QNamespaceStart(Qui)

class QLabel
	: public QDrawCellBase
{
	private:
		QLabelTextPosition text_position;
		QImage             background_image;

	private:
		QSize_t            label_size;
		QWidget*           control_parent;

		QFontDrawCell      font_draw_cell;

	private:
		IMAGE              label_image;

	private:
		bool               is_showing = false;

	private:
		std::wstring       label_text;

	public:
		QLabel(QWidget* parent, QSize_t size)
		{
			label_image = IMAGE(size.width, size.height);

			get_now_attribute();
			control_parent = parent;

			control_parent->bind_contorl(this, &control_id);

			set_fill_color(-1);
			set_line_color(-1);

			label_text    = L"";

			font_draw_cell.set_x(size.x);
			font_draw_cell.set_y(size.y);
			font_draw_cell.set_width(size.width);
			font_draw_cell.set_height(size.height);

			font_draw_cell.reset_image();

			font_draw_cell.put_image(false);

			set_size(&size);
		}
		QLabel(QWidget* parent, QSize_t size, std::wstring text, 
			QLabelTextPosition position = TextLocatedLeft)
		{
			label_image = IMAGE(size.width, size.height);

			get_now_attribute();
			control_parent = parent;

			control_parent->bind_contorl(this, &control_id);

			set_fill_color(-1);
			set_line_color(-1);

			font_draw_cell.set_position(position);
			label_text    = text;
			font_draw_cell.set_text(text);

			font_draw_cell.set_x(size.x);
			font_draw_cell.set_y(size.y);
			font_draw_cell.set_width(size.width);
			font_draw_cell.set_height(size.height);

			font_draw_cell.reset_image();

			font_draw_cell.put_image(false);

			set_size(&size);
		}

	public:
		void set_background_image(QImage image)
		{
			background_image = image;

			if (is_show() == true)
			{
				move(get_x(), get_y());
				draw();
			}
		}
		void set_text(LPCTSTR text, bool repaint = true)
		{
			font_draw_cell.set_text(text);

			if (is_show() == true &&
				repaint == true)
			{
				move(get_x(), get_y());
			}
		}

		bool is_show()
		{
			return is_showing;
		}

		void set_text_flag(QLabelTextPosition position)
		{
			font_draw_cell.set_position(position);

			if (is_show() == true)
			{
				move(get_x(), get_y());
			}
		}
		void set_text_style(LOGFONT font_style)
		{
			font_draw_cell.set_font_style(font_style);

			if (is_show() == true)
			{
				move(get_x(), get_y());
			}
		}

	public:
		void draw()
		{
			is_showing = true;

			if (background_image.empty() == false)
			{
				QPutimage(0, 0, &background_image, 255, &label_image);
			}

			font_draw_cell.draw();
			IMAGE temp_image = font_draw_cell.get_font_image();

			QPutimage(0, 0, &temp_image, 255, &label_image);
			QPutimage(get_x(), get_y(), &label_image);
		}

	public:
		void set_geometry(QSize_t new_geometry)
		{
			label_image = IMAGE(new_geometry.height, new_geometry.width);

			set_height(new_geometry.height);
			set_width (new_geometry.width);
			set_x     (new_geometry.x);
			set_y     (new_geometry.y);

			font_draw_cell.set_x(new_geometry.x);
			font_draw_cell.set_y(new_geometry.y);
			font_draw_cell.set_width(new_geometry.width);
			font_draw_cell.set_height(new_geometry.height);

			font_draw_cell.reset_image();

			if (is_show() == true)
			{
				QSize_t temp_size = get_size();
				qsize_t new_id;

				control_parent->move_repaint(control_id, get_size(), temp_size,
					&new_id, true, this);

				control_id = new_id;
			}
		}

		void show()
		{
			draw();
		}

		void move(int x, int y)
		{
			QSize_t temp_size = get_size();
			qsize_t new_id;

			set_x(x);
			set_y(y);

			control_parent->move_repaint(control_id, get_size(), temp_size,
				&new_id, true, this);

			control_id = new_id;
		}

		void hide()
		{
			qsize_t new_id;

			control_parent->move_repaint(control_id, get_size(), get_size(),
				&new_id, false, this);

			control_id = new_id;
		}
};

QNamespaceEnd